import bluebird from 'bluebird';
import db from '../db';
import * as util from '../../../util/index';

export function fetch(query, op, pageOpt, user) {
    return db.get('xp_activity').query(qb => {
        if (query) {
            util.andWhere(query, op, qb);
        }
        qb.distinct('xp_activity.*');
        //统计保单数
        const t1 = db._.raw('select count(1) from xp_policy where xp_policy.activityId=xp_activity.id ').wrap('(', ') policyCount');
        const t2 = db._.raw('select sum(money) from xp_policy where xp_policy.activityId=xp_activity.id ').wrap('IFNULL((', '), 0) completedMoney');
        //已录客户总数
        const t3 = db._.raw('select count(1) from xp_customers where xp_customers.activityId=xp_activity.id ').wrap('(', ') activityCustomerCount');
        qb.select(t1, t2, t3);
        //当前用户在该活动中录入的客户数
        if (user) {
            const t4 = db._.raw(`select count(1) from xp_customers where xp_customers.activityId=xp_activity.id and xp_customers.salesmanId='${user.id}'`).wrap('(', ') myCustomerCount');
            qb.select(t4);
        }
        qb.orderBy('createTime', 'desc');
    }).fetchPage({
        pageSize: pageOpt.pageSize,
        page: pageOpt.page,
        withRelated: ['institution']
    });
}

export function find(query, op, user) {
    return db.get('xp_activity').query(qb => {
        if (query) {
            util.andWhere(query, op, qb);
        }
        qb.distinct('xp_activity.*');
        //统计保单数
        const t1 = db._.raw('select count(1) from xp_policy where xp_policy.activityId=xp_activity.id ').wrap('(', ') policyCount');
        const t2 = db._.raw('select sum(money) from xp_policy where xp_policy.activityId=xp_activity.id ').wrap('IFNULL((', '), 0) completedMoney');
        //已录客户总数
        const t3 = db._.raw('select count(1) from xp_customers where xp_customers.activityId=xp_activity.id ').wrap('(', ') activityCustomerCount');
        qb.select(t1, t2, t3);
        //当前用户在该活动中录入的客户数
        if (user) {
            const t4 = db._.raw(`select count(1) from xp_customers where xp_customers.activityId=xp_activity.id and xp_customers.salesmanId='${user.id}'`).wrap('(', ') myCustomerCount');
            qb.select(t4);
        }
        qb.orderBy('createTime', 'desc');
    }).list({
        withRelated: [{
            images: qb => {
                qb.orderBy('displayOrder', 'asc');
            }
        },
            'institution'
        ]
    });
}

export function add(data, images) {
    return db.exec(model => model('xp_activity').forge().save(data, {method: 'insert'})
        .then(() => bluebird.reduce(images, (total, item) => model('xp_activityimages')
            .forge()
            .save(item, {method: 'insert'}), 0)));
}

export function edit(data, images) {
    return db.exec(model => model('xp_activity').where({id: data.id}).save(data, {method: 'update'})
        .then(() => model('xp_activityimages').where({activityId: data.id}).drop())
        .then(() => bluebird.reduce(images, (total, item) => model('xp_activityimages')
            .forge()
            .save(item, {method: 'insert'}), 0)));
}

export function del(id) {
    return db.exec(model => model('xp_activityimages')
        .where({activityId: id})
        .drop().then(() => model('xp_activity')
            .forge({id})
            .drop()));
}
